package com.kx.demo.concurrent;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kx
 */
public class ReentractLockDemo {

    static volatile int i = 0;
    static ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) {

        new Thread(){
            @Override
            public void run() {
                for (int i1 = 0; i1 < 100000; i1++) {
//                    lock.lock();
                    i++;
//                    System.out.println(i);
//                    lock.unlock();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                for (int i1 = 0; i1 < 100000; i1++) {
                    lock.lock();
//                    lock.tryLock(1, TimeUnit.SECONDS);
                    i++;
//                    System.out.println(i);
                    lock.unlock();
                }
            }

        }.start();
        try {
            Thread.sleep(2*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Scanner in = new Scanner(System.in);

        System.out.println(i);
    }
}
